<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author LYS
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleExport()" v-auth="'bus:cancel:yfCancelRecord:view'">
          <Icon icon="ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <a-button type="primary" @click="handleModel()" v-auth="'bus:cancel:yfCancelRecord:modify'">
          <Icon icon="ant-design:edit-outlined" /> {{ t('修改') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.remarks }}
        </a>
      </template>
      <template #uploadFile="{ record }">
        <!-- disabled -->
        <BasicUpload
          v-model:value="record.dataMap"
          :bizKey="record.id"
          :bizType="'yf_cancel_file'"
          disabled
          :uploadType="'image'"
          :loadTime="record.__t"
          :showPreview="true"
          :emptyHidePreview="true"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BasicModal
      v-bind="$attrs"
      :title="t('修改')"
      @register="registerModal"
      @ok="handleUpdate"
      :minHeight="90"
      :width="500"
    >
      <BasicForm @register="registerFormDate" />
    </BasicModal>
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsBusCancelYfCancelRecordList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, nextTick } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import {
    yfCancelRecordDelete,
    yfCancelRecordListData,
    yfCancelRecordUpdate,
  } from '/@/api/bus/cancel/yfCancelRecord';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BasicUpload } from '/@/components/Upload';
  import { usePermission } from '/@/hooks/web/usePermission';
  import { m8ViewRouteTreeData } from '/@/api/bas/route/m8ViewRoute';
  import { downloadByUrl2 } from '/@/utils/file/download';
  import { useGlobSetting } from '/@/hooks/setting';
  import { BasicModal, useModal } from '/@/components/Modal';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { empUserTreeData } from '/@/api/sys/empUser';

  const { hasPermission } = usePermission();

  const { t } = useI18n('bus.cancel.yfCancelRecord');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('收运失败记录'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: [
      {
        label: t('时间'),
        field: 'createDate',
        component: 'RangePicker',
      },
      {
        label: t('医疗机构'),
        field: 'hospName',
        component: 'Input',
      },
      {
        label: t('线路'),
        field: 'm8ViewHosp.routeCode',
        fieldLabel: 'm8ViewHosp.croute',
        component: 'TreeSelect',
        componentProps: {
          api: m8ViewRouteTreeData,
          params: { isShowCode: false },
          // canSelectParent: false,
          allowClear: true,
        },
        ifShow: hasPermission(['data:route:show']),
      },
      {
        label: t('原因'),
        field: 'remarks',
        component: 'Input',
      },
    ],
    fieldMapToTime: [['createDate', ['createDate_gte', 'createDate_lte']]],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('医疗机构'),
      dataIndex: 'hospName',
      key: 'a.hosp_name',
      sorter: true,
      width: 330,
      align: 'left',
      fixed: 'left',
    },
    {
      title: t('收运人'),
      dataIndex: 'creator.userName',
      key: 'u1.user_code',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('时间'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('线路'),
      dataIndex: 'm8ViewHosp.croute',
      key: 'hosp.croute',
      sorter: true,
      width: 130,
      align: 'left',
      ifShow: hasPermission(['data:route:show']),
    },
    {
      title: t('线路顺序'),
      dataIndex: 'm8ViewHosp.routeSort',
      key: 'hosp.route_Sort',
      sorter: true,
      width: 100,
      align: 'left',
      ifShow: hasPermission(['data:route:show']),
    },
    {
      title: t('驾驶员'),
      dataIndex: 'driverName',
      key: 'a.driver_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('押运员'),
      dataIndex: 'driver2Name',
      key: 'a.driver2_name',
      sorter: true,
      width: 80,
      align: 'left',
    },
    {
      title: t('车牌号'),
      dataIndex: 'carNo',
      key: 'a.car_no',
      sorter: true,
      width: 80,
      align: 'center',
    },
    {
      title: t('原因'),
      dataIndex: 'remarks',
      key: 'a.remarks',
      sorter: true,
      width: 230,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('现场照片'),
      dataIndex: 'uploadFile',
      width: 130,
      align: 'center',
      slot: 'uploadFile',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 80,
    actions: (record: Recordable) => [
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除收运失败记录'),
        popConfirm: {
          title: t('是否确认删除收运失败记录'),
          confirm: handleDelete.bind(this, { id: record.id, preId: record.preId }),
        },
        auth: 'bus:cancel:yfCancelRecord:delete',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRowKeys }] = useTable({
    api: yfCancelRecordListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    ellipsis: false,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });
  const [registerModal, { openModal, closeModal }] = useModal();
  const inputFormSchemas: FormSchema[] = [
    {
      label: t('驾驶员'),
      field: 'driverName',
      fieldLabel: 'driverName',
      component: 'TreeSelect',
      componentProps: {
        api: empUserTreeData,
        params: { officeCode: '01002', isShowCode: false },
        canSelectParent: false,
        allowClear: true,
      },
      required: true,
    },
    {
      label: t('押运员'),
      field: 'driver2Name',
      fieldLabel: 'driver2Name',
      component: 'TreeSelect',
      componentProps: {
        api: empUserTreeData,
        params: { officeCode: '01002', isShowCode: false },
        canSelectParent: false,
        allowClear: true,
      },
    },
    {
      label: t('车牌号'),
      field: 'carNo',
      component: 'Input',
      componentProps: {},
    },
  ];

  const [registerFormDate, { validate: dateValidate, setFieldsValue: setFormDate }] = useForm({
    labelWidth: 90,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await yfCancelRecordDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl2({
      url: ctxAdminPath + '/bus/cancel/yfCancelRecord/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  async function handleModel() {
    if (getSelectRowKeys().length) {
      openModal(true, {});
      await nextTick();
      setFormDate([{ driverName: '' }, { driver2Name: '' }]);
    } else {
      showMessage('请选择一条记录');
    }
  }

  // 修改驾驶员、押运员
  async function handleUpdate() {
    const selIds = getSelectRowKeys().join(',');
    const data = await dateValidate();
    const res = await yfCancelRecordUpdate({ selIds: selIds, ...data });
    closeModal();
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }
</script>
